
#include "SocketOps.h"

namespace SocketOps
{
    SOCKET CreateTCPFileDescriptor()
    {
        return socket(AF_INET, SOCK_STREAM, 0);
    }

    bool Nonblocking(SOCKET fd)
    {
        int arg = 1;
        return (::ioctl(fd, FIONBIO, &arg) == 0);
    }

    bool Blocking(SOCKET fd)
    {
        int arg = 0;
        return (ioctl(fd, FIONBIO, &arg) == 0);
    }

    bool DisableBuffering(SOCKET fd)
    {
        int arg = 1;
        return (setsockopt(fd, 0x6, 0x1, (const char*)&arg, sizeof(arg)) == 0);
    }

    bool EnableBuffering(SOCKET fd)
    {
        int arg = 0;
        return (setsockopt(fd, 0x6, 0x1, (const char*)&arg, sizeof(arg)) == 0);
    }

    bool SetSendBufferSize(SOCKET fd,int iSize)
    {
        return (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const char*)&iSize, sizeof(iSize)) == 0);
    }

    bool SetRecvBufferSize(SOCKET fd,int iSize)
    {
        return (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char*)&iSize, sizeof(iSize)) == 0);
    }

    void CloseSocket(SOCKET fd)
    {
        shutdown(fd, SHUT_RDWR);
        close(fd);
    }

    void ReuseAddr(SOCKET fd)
    {
        int option = 1;
        setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&option, 4);
    }
}

